package com.kl.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kl.common.exception.SystemException;
import com.kl.modular.base.service.impl.BaseServiceImpl;
import com.kl.modular.system.dao.mapper.SystemAreaMapper;
import com.kl.modular.system.dao.model.SystemArea;
import com.kl.modular.system.service.api.ISystemAreaService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 行政区域serviceImpl业务实现
 * @author xr_zjb
 * @date 2017-09-06 16:26
 * @(C) xinrui Copyright 
 */
@Service
public class SystemAreaServiceImpl extends BaseServiceImpl<SystemAreaMapper, SystemArea> implements ISystemAreaService {


    public List<SystemArea> getAreaList(Long areaId) throws SystemException {
        EntityWrapper<SystemArea> ew = new EntityWrapper<SystemArea>();
        if (areaId == null)
            throw new SystemException("区域id为空");
        ew.eq("id",areaId);
        SystemArea systemArea = selectOne(ew);
        EntityWrapper<SystemArea> wrapper = new EntityWrapper<SystemArea>();
        Long type = systemArea.getLevel();
        if(type.equals(1l)){
            wrapper.like("position","tr_0 tr_" + areaId);
        } else if(type.equals(2l)){
            wrapper.eq("position","tr_0 tr_" + systemArea.getParentid() + " tr_"+ areaId);
        } else if(type.equals(3l)){
            wrapper.eq("id",areaId);
        }
        return selectList(wrapper);
    }

    public List<SystemArea> getSubAreaList(Long areaId) throws SystemException {
        EntityWrapper<SystemArea> ew = new EntityWrapper<SystemArea>();
        if (areaId == null)
            throw new SystemException("区域id为空");

        ew.eq("parentid",areaId);
        return selectList(ew);
    }
}
